package restPROC;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import utilClasses.ListaUser;
import dao.daoKonekcija;
import dao.daoKorisnik;
import dao.daoPermisije;





@Path("/users")  
public class Users {
	@GET
	@Path("/logins")
	@Produces({"application/xml"})
	public ListaUser getUsers(@Context HttpServletRequest req)
	{
		
		ListaUser rez = new ListaUser();
		HttpSession sess = req.getSession(true);
		String korisnik=sess.getAttribute("korisnik").toString();
		System.out.println(korisnik);
		try {
			boolean jelima=daoPermisije.provjeri(korisnik, "getUsers");
			System.out.println(jelima);
			if(!jelima) return rez;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/* -- PROBA
		rez.getUsers().add(new uLogin("Irfan"));
		rez.getUsers().add(new uLogin("abcd"));
		rez.getUsers().add(new uLogin("user1"));
		rez.getUsers().add(new uLogin("Amer"));*/
		try {
			
			rez=daoKorisnik.dajSveUsere();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	@GET
	@Path("/filter")
	@Produces({"application/xml"})
	public ListaUser filterUsers(@QueryParam("filter") String filter,@Context HttpServletRequest req)
	{
		ListaUser rez = new ListaUser();
		HttpSession sess = req.getSession(true);
		String korisnik=sess.getAttribute("korisnik").toString();
		System.out.println(korisnik);
		try {
			boolean jelima=daoPermisije.provjeri(korisnik, "filterUsers");
			System.out.println(jelima);
			if(!jelima) return rez;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			rez=daoKorisnik.dajUsereLike(filter+"%");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
	}
	@GET
	@Path("/test")
	public Response getTest()
	{
		return Response.status(200).entity("Radi!!!").build();
	}

}
